<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Catcont extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	public function __construct(){
		parent::__construct();
		$this->load->library("my_layout");
		$this->my_layout->setLayout("layout/layout_sub");
		
		$this->load->library("form_validation");
		$this->load->library('session');
		$this->load->model('app_model');
	}
	private $limit = 18;
	public function getCatSubId($ref =NULL){
		
		$data = $this->app_model->showDetailId($ref);
		$setview = $data[0];
		$setview['relation'] = $this->app_model->showRelationId($ref);
		$this->my_layout->view('html/productdetail',$setview);
	}
	public function getCatId($ref =NULL){
		
		$this->load->helper('url');		
		
		//Get Offset		
		if($this->uri->segment(4) == FALSE)
			$offset = 0;
		else
			$offset = $this->uri->segment(4);
		//Load data		
		$data['subcate'] =$this->app_model->getSubcateId($ref,$this->limit, $offset);
		
		//Declare CodeIgniter Pagination
		$this->load->library('pagination');
		
		//Set Config Pagination
		$config['base_url'] = site_url('catcont/getCatId/'.$ref);
 		$config['total_rows'] = $this->app_model->count_all($ref);
 		$config['per_page'] = $this->limit;
 		
 		$config['full_tag_open'] = '<p>';
		$config['full_tag_close'] = '</p>';
		
 		$config['first_link'] = 'First';
 		$config['first_tag_open'] = '<div>';
 		$config['first_tag_close'] = '</div>';
// 		
// 		$config['next_link'] = '&gt;';
//		$config['next_tag_open'] = '<div>';
//		$config['next_tag_close'] = '</div>';
// 		
// 		$config['prev_link'] = '&lt;';
//		$config['prev_tag_open'] = '<div>';
//		$config['prev_tag_close'] = '</div>';
 		
 		$config['last_link'] = 'Last';
 		$config['last_tag_open'] = '<div>';
 		$config['last_tag_close'] = '</div>';
 		
// 		$config['cur_tag_open'] = '<b>';
//		$config['cur_tag_close'] = '</b>';
 		
// 		$config['num_tag_open'] = '<div>';
//		$config['num_tag_close'] = '</div>';
 		
// 		$config['display_pages'] = FALSE; //an cac so trang tren view
 		
		$config['uri_segment'] = 4;
		$this->pagination->initialize($config);
		
//		$this->my_layout->setLayout("layout/layout_content");
//		$data['subcate'] = $this->app_model->getSubcateId($ref);
		$data['name'] = $this->app_model->getCateGoryName_SubId($ref);
		$this->my_layout->view('html/catcommon',$data);
	}
	public function showSubCatId($id = null,$name = null){
		if ($id){
			$data['subcate'] = $this->app_model->getCateGory_SubId($id);
			$limit = count($data['subcate']);
			for($i=0;$i<$limit;$i++){
				$data['subcate'][$i]['images'] = $this->app_model->getImgTypeSub($data['subcate'][$i]['cat_type_sub_id']);
			}
			$order = array('_','/');
	  		$data['name'] = str_replace($order, " ", $name); 
			$this->my_layout->view('html/catsub',$data);
		}
	}
	public function customerservices (){
		$this->my_layout->view('html/customerservices');
	}
	public function aboutus (){
		if (isset($this->session->userdata['lang'])){
			if($this->session->userdata['lang'] == 'vi'){
				$this->my_layout->view('html/aboutusvi');
			}else{
				$this->my_layout->view('html/aboutus');
			}
		}else{
			$this->my_layout->view('html/aboutus');
		}
	}
	public function contactus (){
		if (isset($this->session->userdata['lang'])){
			if($this->session->userdata['lang'] == 'vi'){
				$this->my_layout->view('html/contactus');
			}else{
				$this->my_layout->view('html/contactusen');
			}
		}else{
			$this->my_layout->view('html/contactusen');
		}
	}
	public function sendMailContact(){
		$this->form_validation->set_rules('email', 'Email','trim|required|valid_email|max_length[100]|xss_clean');
		$this->form_validation->set_rules('name', 'Name','trim|min_length[8]|required|xss_clean');
		$this->form_validation->set_rules('content_text', 'content_text','trim|min_length[20]|required|xss_clean');
		$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
		if($this->form_validation->run()){
			$data= array(
				'name' => $this->input->post('name'),
				'phone' => $this->input->post('tel'),
				'email' => $this->input->post('email'),
				'company' => $this->input->post('company'),
			);
			$this->sendmail_smtp($this->input->post('content_text'),$data);
			redirect(base_url(),'post',false);
		}else{
			$this->my_layout->view('html/contactus');
		}
	}
	private function sendmail_smtp($body =null,$data=null){

		$this->load->library('email');

		$config['protocol'] = 'smtp';
		$config['smtp_host'] = 'mail.sagaco.com.vn';
		$config['smtp_port'] = '25';
		$config['smtp_timeout'] = '5';
		$config['smtp_user'] = 'admin@sagaco.com.vn';
		$config['smtp_pass'] = 'Thanh_Addmin';//'119thanh782';
		$config['charset'] = 'utf-8';
		$config['wordwrap'] = TRUE;

		$this->email->initialize($config);

		$this->email->from('nvthanhdhkhtn@gmail.com', 'Sagaco');
		
		$this->email->to('tonychau@sagaco.com.vn');
		$this->email->cc('nvthanhdhkhtn@gmail.com');
//		$this->email->cc('sagafurniture@gmail.com');

		$this->email->subject('Email Contact');
		$this->email->set_newline("\r\n");
		$this->email->set_newline("\r\n");
		
		$this->email->message("Name: ".$data['name']."\r\n Phone: ".$data['phone']."\r\n Email: ".$data['email']."\r\n Company: ".$data['company']."\r\n ".$body);
		
		if($this->email->send()){
			return true;
		}
		return false;
		echo $this->email->print_debugger();
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */